<template>
	<div class="content">
		<Collapse v-model="value1">
			<Panel name="1">
				释放规则配置
			 
				<p slot="content">
                       <section v-for="(item,index) in btn_data" :key="index">
                           <span>{{item.accountName}}</span>
                            <Button  type="primary"  :disabled="item.value == 2?false:true"  @click="clickBtn(item.accountTypeId,item.code)">{{item.value == 1?"进行中":(item.value == 2?'开始':'已结束')}}</Button>
                       </section>
					<Form label-position="right" :label-width="100">
						<div class="canshu"  v-for="(item,index) of config" :key="index++">
							<FormItem label="释放规则：">
								周期
								<Input class="w80" v-model="item.day" type="number" number/>天&nbsp;&nbsp;释放
								<Input class="w80" v-model="item.ratio" type="number" number/> %
								<Icon style="margin-left: 10px;cursor: pointer;margin-right: 10px;" type="ios-add-circle" size="21" color="#2d8cf0" :class="index != config.length ? 'visibility' : ''" @click="addRatios"></Icon>
								<Icon style="cursor: pointer;margin-right: 10px;" type="ios-remove-circle" size="21" color="rgb(237, 64, 20)" :class="index != config.length ? 'visibility' : ''"  @click="delRatios(index)"></Icon>
								
							</FormItem>
						</div>
						<div class="canshu">
							<FormItem>
								<font color="#ed4014">*释放百分比加起来须等于100%</font>
							</FormItem>
						</div>
					</Form>
				</p>
				<p slot="content">
					<Form label-position="right" :label-width="100">
						<div class="canshu">
							<div class="pl-100">
								<Button type="primary" size="large" v-on:click="btn">修 改</Button>
							</div>
						</div>
					</Form>
				</p>
			</Panel>
		</Collapse>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				value1: '1', // 默认展开第一个列表
				config: [],
				dataValue: '',
				token: localStorage.mytoken,
                minPool: '',
                btn_data:[]
			}
		},
		mounted() {
			this.$Message.config({
				top: 250
			});
            this.getOther(); // 其他配置查询
            this.getBtn();//按钮
		},
		methods: {
            // 点击调整释放状态
            clickBtn(paraId,paraCode) {
                let params = this.qs.stringify({
                    accountTypeId:paraId,
                    code:paraCode
                }); 
                this.axios.post('/admin/settings/release/openButton', params,{
					headers: {
						Authorization: "bearer " + this.token
					}
				}).then(res => {                  
					if(res.data.code == 200) {
                        this.$Message.info(res.data.msg)
                        this.getBtn();
					}
				})
            },
            getBtn() {
                this.axios.get('/admin/settings/release/openconfig', {
					headers: {
						Authorization: "bearer " + this.token
					}
				}).then(res => {                  
					if(res.data.code == 200) {
                        this.btn_data = res.data.data;
					}
				})
            },
			// 处理时间段时间格式
			getTime(daterange) {
				this.dataValue = daterange;
			},
			addRatios() {
				this.config.push({
					day: '',
					ratio: 0
				})
			},
			delRatios(item) {
				let $this = this;
				this.$Modal.confirm({
					title: '删除配置',
					content: '是否要删除配置？',
					onOk: function() {
                      
                      $this.config.splice(item-1,1)
					}
				});
			},
			updateRatios(item) {
				let ratio = 0;
				let params = [];
				this.config.ratio.forEach((row, i) => {
					ratio += Number(row.value);
					params.push({
						code: row.code,
						id: row.id,
						r1: row.r1,
						value: row.value / 100
					})
				})
				if(!item.r1 || !item.value) {
					this.$Message.info('值不能为空');
					return false;
				}
				if(ratio != 100) {
					this.$Message.info('释放百分比加起来须等于100%');
					return false;
				}
				this.axios.post('/admin/settings/aladdin_plan_config/batchProcess', params, {
					headers: {
						Authorization: "bearer " + this.token
					}
				}).then(res => {
					if(res.data.code == 200) {
						this.$Message.info(res.data.msg);
					} else if(res.data.code == 500) {
						this.$Message.info(res.data.msg);
					} else if(res.data.code == 401) {
						this.$Message.info(res.data.msg);
						localStorage.removeItem("mytoken");
						this.$router.push({
							name: 'login'
						})
					}
				})
			},
			getOther() {
				this.axios.get('/admin/settings/release/config', {
					headers: {
						Authorization: "bearer " + this.token
					}
				}).then(res => {
                    
					if(res.data.code == 200) {
                        this.config = res.data.data;
                        this.config.forEach((row, index) => {
                          
							this.config[index].ratio = row.ratio * 100;
						})

					} else if(res.data.code == 500) {
						this.$Message.info(res.data.msg);
					} else if(res.data.code == 401) {
						this.$Message.info(res.data.msg);
						localStorage.removeItem("mytoken");
						this.$router.push({
							name: 'login'
						})
					}
				})
			},
			btn(){
                let that =this;
                let params = [];
                
               for(let i=0;i<this.config.length;i++) {
                   
                   let num = Number(this.config[i].ratio).div(100);
                   // 循环判断是否符合提交条件      
                   if(Number(this.config[i].ratio) <= 0) {
                        this.$Message.info('百分比不能小于或等于0');
                        return false;
                    }
                    if(this.config[i].day == '') {
                        this.$Message.info('周期不能为空');
                        return false;
                    }
                    params.push({'day':this.config[i].day,'ratio':num});
               }
                let num = 0;
                this.config.forEach(item=>{
                    num += item.ratio   
                })   
                if(num!=100) {
                    this.$Message.info('释放百分比加起来须等于100%')
                    return false;
                }
				this.axios.post('/admin/settings/release/config', params, {
					headers: {
						Authorization: "bearer " + this.token
					}
				}, {
					arrayFormat: 'indices',
					allowDots: true
				}).then(res => {
					if(res.data.code == 200) {
						this.$Message.info(res.data.msg);
                        this.getOther();
					} 
				})
			}
		}
	}
</script>

<style lang="less" scoped>
	.p-10 {
		padding-top: 0 !important;
		padding-left: 50px !important;
	}
	
	.w50 {
		width: 50px !important;
	}
	
	.w25 {
		width: 25px !important;
	}
	
	.w120 {
		width: 120px !important;
	}
	
	.mar {
		margin-left: 5px;
	}
	
	.pl-100 {
		padding-left: 100px;
	}
	
	.visibility {
		visibility: hidden;
    }
    .content section {
        display: flex;
        width: 180px;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 10px;
        margin-left: 28px;
        
    }
    .content section button {
        width: 60px;
        text-align: center;
        padding: 5px  0;
    }
</style>